﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

using Carmall.Entity;
using Carmall.BLL;

namespace Carmall.Web.Models
{
    #region ViewModel
    public class LogOnModel
    {
        [Required]
        [DisplayName("用户名")]
        public string UserName { get; set; }

        [Required]
        [DataType(DataType.Password)]
        [DisplayName("密码")]
        public string Password { get; set; }
    }
    #endregion

    #region Services
    public interface IAccountUserService
    {
        int MinPasswordLength { get; }

        UsersInfo IsExist(string userName);
        UsersInfo ValidateUser(string userName, string password);
        string CreateUser(UsersInfo user);
        bool ChangePassword(int userId, string oldPassword, string newPassword);
    }

    public class AccountUserServices : IAccountUserService
    {
        #region IUserService 成员

        public int MinPasswordLength
        {
            get { return 4; }
        }

        public UsersInfo IsExist(string userName)
        {
            if (String.IsNullOrEmpty(userName)) throw new ArgumentException("值不能为 null 或为空。", "userName");
            string cond = String.Format("Username='{0}'", userName);
            return BLLFactory<Users>.Instance.FindSingle(cond);
        }

        public UsersInfo ValidateUser(string userName, string password)
        {
            if (String.IsNullOrEmpty(userName)) throw new ArgumentException("值不能为 null 或为空。", "userName");
            if (String.IsNullOrEmpty(password)) throw new ArgumentException("值不能为 null 或为空。", "password");

            //return ValidateUser(userName, password);
            string cond = String.Format("Username='{0}' AND Password='{1}'", userName, password);
            return BLLFactory<Users>.Instance.FindSingle(cond);
        }

        public string CreateUser(UsersInfo user)
        {
            throw new NotImplementedException();
        }

        public bool ChangePassword(int userId, string oldPassword, string newPassword)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public interface IFormsAuthenticationService
    {
        void SignIn(string userName, bool createPersistentCookie);
        void SignOut();
    }

    public class FormsAuthenticationService : IFormsAuthenticationService
    {
        public void SignIn(string userName, bool createPersistentCookie)
        {
            if (String.IsNullOrEmpty(userName)) throw new ArgumentException("值不能为 null 或为空。", "userName");

            FormsAuthentication.SetAuthCookie(userName, createPersistentCookie);
        }

        public void SignOut()
        {
            FormsAuthentication.SignOut();
        }
    }

    #endregion
    #region Helper
    public static class AccountHelper
    {
        public static List<ModulesInfo> GetModules()
        {
            if (HttpContext.Current.Cache["Modules"] == null)
            {
                var items = BLLFactory<Modules>.Instance.GetAll();
                HttpContext.Current.Cache["Modules"] = items;
            }

            return (List<ModulesInfo>)HttpContext.Current.Cache["Modules"];
        }

        public static Int32[] GetUserPermission(int UserId)
        {
            if (HttpContext.Current.Cache["Permissmin"] == null)
            {
                RolesInfo r = BLLFactory<Carmall.BLL.Roles>.Instance.FindSingle("UserID=" + UserId);
                string[] ids = r.RolePermission.Split(',');
                Int32[] pers = new Int32[ids.Length];
                for (int i = 0; i < pers.Length; i++)
                {
                    pers[i] = SafeType.SafeInt(ids[i]);
                }
                HttpContext.Current.Cache["Permissmin"] = pers;
            }
            return (Int32[])HttpContext.Current.Cache["Permissmin"];
        }
    }
    #endregion

}